﻿using System;
using System.Globalization;
using System.Linq;
using System.Security.Claims;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using System.Data;
using System.Reflection;
using System.Web.SessionState;
using app.Api;

namespace app.Controllers
{
    public class ApiController : Controller
    {
        private bool use_try = true;
        // 接口注册
        private void Apis(string action1, string action2)
        {
            switch (action1)
            {
                case "article": new ApiArticle(this).Do(action2); break;
                case "role": new ApiRole(this).Do(action2); break;
                case "search": new ApiSearch(this).Do(action2); break;
                case "admin": new ApiLogin(this).Admin(action2); break;
                case "member": new ApiLogin(this).Member(action2); break;
                case "me": new ApiLogin(this).Me(action2); break;
                case "database": new ApiDatabase(this).Do(action2); break;
                case "upload": new ApiUpload(this).Do(action2); break;
                case "investment": new ApiInvestment(this).Do(action2); break;
                case "abutment": new ApiAbutment(this).Do(action2); break;
                case "check": new ApiCheck(this).Do(action2); break;
                default: new ApiError(this).ErrorUrl(action1); break;
            }
        }

        // 入口，并获取异常
        public void Do(string action1, string action2)
        {
            if(use_try)
            {
                try
                {
                    Apis(action1, action2);
                }
                catch (ApiException e)
                {
                    // 由api自动抛出的异常，不做处理
                }
                catch (Exception e)
                {
                    // 显示程序异常
                    new ApiError(this).HasException(e);
                }
            }
            else
            {
                Apis(action1, action2);
            }
        }

        // 接口测试页面
        public ActionResult ApiTest()
        {
            return View();
        }
    }
}